﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MySuperTank
{
    public partial class MainMenu : Form
    {
        [DllImport("user32.dll")]
        private static extern bool PostMessage(IntPtr hWnd, int msg, long wParam, long lParam);

        private Point prevLeftClick;
        private bool isFirst = true;
        private bool toBlock = true;

        public MainMenu()
        {
            InitializeComponent();
        }

        private void MainMenu_Load(object sender, EventArgs e)
        {
            BitmapRegion.CreateControlRegion(this, new Bitmap("mainmenu.gif"));

            BitmapRegion.CreateControlRegion(button1, new Bitmap("newgame00.bmp"));
            BitmapRegion.CreateControlRegion(button2, new Bitmap("loadgame00.bmp"));
            BitmapRegion.CreateControlRegion(button3, new Bitmap("quit00.bmp"));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Program.isMainFormOpen == false)
            {
                MainForm mf = new MainForm();
                mf.Show();
                Program.isMainFormOpen = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //load dữ liệu từ file xml
            XmlDocument save = new XmlDocument();
            save.Load("Save.xml");
            XmlElement node = save.DocumentElement;
            XmlElement map = (XmlElement)node.SelectSingleNode("map");

            //gán map
            Program.iCurMap = int.Parse(map.InnerText);

            //bật form lên
            if (Program.isMainFormOpen == false)
            {
                MainForm mf = new MainForm();
                mf.Show();
                Program.isMainFormOpen = true;
            }
        }

        private void MainMenu_MouseMove(object sender, MouseEventArgs e)
        {
            // Check if dragging of the form has occurred
            if (e.Button == MouseButtons.Left)
            {
                // If this is the first mouse move event for left click dragging of the form,
                // store the current point clicked so that we can use it to calculate the form's
                // new location in subsequent mouse move events due to left click dragging of the form
                if (isFirst == true)
                {
                    // Store previous left click position
                    prevLeftClick = new Point(e.X, e.Y);

                    // Subsequent mouse move events will not be treated as first time, until the
                    // left mouse click is released or other mouse click occur
                    isFirst = false;
                }

                // On subsequent mouse move events with left mouse click down. (During dragging of form)
                else
                {
                    // This flag here is to allow alternate processing for dragging the form because it
                    // causes serious flicking when u allow every such events to change the form's location.
                    // You can try commenting this out to see what i mean
                    if (toBlock == false)
                        this.Location = new Point(this.Location.X + e.X - prevLeftClick.X, this.Location.Y + e.Y - prevLeftClick.Y);

                    // Store new previous left click position
                    prevLeftClick = new Point(e.X, e.Y);

                    // Allow or deny next mouse move dragging event
                    toBlock = !toBlock;
                }
            }

            // This is a new mouse move event so reset flag
            else
                isFirst = true;
        }

        private void MainMenu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                const int HTCAPTION = 2;
                const int WM_NCLBUTTONDOWN = 161;

                int ix = e.X & 0xffff;
                int iy = e.Y & 0xffff;
                long ll = ix | iy << 16;

                PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, ll);
            }
        }

    }
}
